package com.mcu32.bright.service.impl;

import cn.hutool.http.Method;
import cn.hutool.json.JSONUtil;
import com.mcu32.bright.config.OneNetProperties;
import com.mcu32.bright.model.Result;
import com.mcu32.bright.model.bo.CreateDeviceBo;
import com.mcu32.bright.model.bo.DevicePropertyBo;
import com.mcu32.bright.model.bo.DevicePropertyQueryBo;
import com.mcu32.bright.service.OneNetService;
import com.mcu32.bright.util.OneNetUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class OneNetServiceImpl implements OneNetService {

    @Autowired
    private OneNetUtil oneNetUtil;
    @Autowired
    private OneNetProperties oneNetProperties;

    @Override
    public Result createDevice(String deviceName) {
        String productId = oneNetProperties.getCommonInfo().getProductId();
        OneNetProperties.DeviceManagement deviceManagement = oneNetProperties.getDeviceManagement();
        CreateDeviceBo createDeviceBo = new CreateDeviceBo(productId, deviceName);
        String requestStr = JSONUtil.toJsonStr(createDeviceBo);
        return oneNetUtil.invoke(deviceManagement.getNameSpace(), deviceManagement.getCreateDeviceAction(), requestStr , Method.POST);
    }

    @Override
    public Result setDeviceProperty(DevicePropertyBo devicePropertyBo) {
        String requestStr = JSONUtil.toJsonStr(devicePropertyBo);
        OneNetProperties.ApplicationDevelopment applicationDevelopment = oneNetProperties.getApplicationDevelopment();
        String nameSpace = applicationDevelopment.getNameSpace();
        String setDevicePropertyAction = applicationDevelopment.getSetDevicePropertyAction();
        return oneNetUtil.invoke(nameSpace, setDevicePropertyAction, requestStr , Method.POST);
    }

    @Override
    public Result queryDeviceProperty(DevicePropertyBo devicePropertyBo) {
        String requestStr = JSONUtil.toJsonStr(devicePropertyBo);
        OneNetProperties.ApplicationDevelopment applicationDevelopment = oneNetProperties.getApplicationDevelopment();
        String nameSpace = applicationDevelopment.getNameSpace();
        String queryDevicePropertyAction = applicationDevelopment.getQueryDevicePropertyAction();
        return oneNetUtil.invoke(nameSpace, queryDevicePropertyAction, requestStr , Method.GET);
    }

    @Override
    public Result queryDevicePropertyDetail(DevicePropertyQueryBo devicePropertyQueryBo) {
        return null;
    }
}
